package com.mondo.financialwallet.presentation.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.common.ui.AbsNetworkStateActivity;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.DataSource;
import com.mondo.financialwallet.data.entity.UserAssetsEntity;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 资产总览
 */
public class AssetOverviewActivity extends AbsNetworkStateActivity {

    @Bind(R.id.cloud_asset_layout) View mCloudAssetsLayout;
    @Bind(R.id.financial_assets_layout) View mFinancialAssetsLayout;
    @Bind(R.id.total_assets_text_view) TextView mTotalAssetsTextView;
    @Bind(R.id.total_amount_available_text) TextView mTotalAmountAvailableText;
    @Bind(R.id.yesterday_earnings_text) TextView mYesterdayEarningsText;
    @Bind(R.id.symbol_cloud_asset_text) TextView mSymbolCloudAssetText;
    @Bind(R.id.financial_assets_text) TextView mFinancialAssetsText;
    @Bind(R.id.transfer_layout) View mTransferLayout;
    @Bind(R.id.receipt_layout) View mReceiptLayout;

    public static void launch(Context context) {
        context.startActivity(new Intent(context, AssetOverviewActivity.class));
    }

    @Override
    protected void setStatusBar() {
        ActivityTitleBarHelper.setTransparentStatus(this);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_assets_overview;
    }

    @Override
    protected void initActions() {
        // 请求用户资产
        requestUserAsset();

        // 标题
        ActivityTitleBarHelper.init(this, R.string.title_asset_manage);

        // 收款
        mReceiptLayout.setOnClickListener(v ->
                TokenReceiptActivity.launch(this));
    }

    private void requestUserAsset() {
        loadingNetwork();

        DataRepository.get().userAssets(result -> {
            successNetwork();

            if (result.isSuccess()) {
                UserAssetsEntity.DataBean asset = result.getData();
                if (asset != null){
                    mTotalAssetsTextView.setText(asset.getTotal_asset());  // 总资产
                    mTotalAmountAvailableText.setText(asset.getAvailable()); // 可用总资产
                    mYesterdayEarningsText.setText("+" + asset.getYesterday_profit());   // 可用余额
                    mFinancialAssetsText.setText(asset.getFund_asset());   // 理财资产
                    mSymbolCloudAssetText.setText(asset.getCloud_asset());

                    // 理财资产
                    mFinancialAssetsLayout.setOnClickListener(v ->
                            FinancialAssetsActivity.launch(this,
                                    asset.getFund_asset(), asset.getYesterday_profit()));

                    // 云端资产
                    mCloudAssetsLayout.setOnClickListener(v ->
                            CloudAssetsActivity.launch(this, asset.getCloud_asset()));
                }
            }
        });
    }

    @Override
    public void onAnewRequest() {
        requestUserAsset();
    }
}
